﻿//-----------------------------------------------------------------------
// <copyright file="ConnectionStateToBrushConverter.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>

namespace ChatBox.UI.ValueConverters
{
    using System;
    using System.Globalization;
    using System.Windows.Data;
    using System.Windows.Media;

    using ChatBox.UI.Controls;

    using DomainObjects;

    /// <summary>
    /// Class to convert a connectionState to a colour in binding
    /// </summary>
    public class ConnectionStateToBrushConverter : IValueConverter
    {
        /// <summary>
        /// The brushes used
        /// </summary>
        private readonly Brush[] brushes = new Brush[5];

        #region IValueConverter Members

        /// <summary>
        /// Convert the value from the connection state enum to a coloured brush
        /// </summary>
        /// <param name="value">the value to convert</param>
        /// <param name="targetType">the target type</param>
        /// <param name="parameter">conversion params</param>
        /// <param name="culture">culture info</param>
        /// <returns>the converted value</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || !(value is ConnectionState))
            {
                return value;
            }

            ConnectionState connectionState = (ConnectionState)value;
            return this.GetBrush(connectionState);
        }

        /// <summary>
        /// Convert back -  not handled
        /// </summary>
        /// <param name="value">the value to convert</param>
        /// <param name="targetType">the target type</param>
        /// <param name="parameter">conversion params</param>
        /// <param name="culture">culture info</param>
        /// <returns>the converted value</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// Get the brush, create the brush as it is needed
        /// </summary>
        /// <param name="state">the state for the brush</param>
        /// <returns>the brush to use</returns>
        private Brush GetBrush(ConnectionState state)
        {
            int index = (int)state;

            if (this.brushes[index] == null)
            {
                Brush newBrush;

                switch (state)
                {
                    case ConnectionState.Unknown:
                        newBrush = new SolidColorBrush(Colors.Brown);
                        break;

                    case ConnectionState.NotConnected:
                        newBrush = new SolidColorBrush(Colors.Black);
                        break;

                    case ConnectionState.Idle:
                        newBrush = new SolidColorBrush(Colors.DarkGreen);
                        break;

                    case ConnectionState.DataReceived:
                        newBrush = new SolidColorBrush(Colors.LimeGreen);
                        break;

                    case ConnectionState.Error:
                        newBrush = new SolidColorBrush(Colors.Red);
                        break;

                    default:
                        throw new ChatBoxException("No brush found for state" + index);
                }

                this.brushes[index] = newBrush;
            }

            return this.brushes[index];
        }
    }
}
